//
//  GoodsListViewController.m
//  Commerce
//
//  Created by gc on 15/3/28.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "GoodsListViewController.h"
#import "GoodsDetailViewController.h"
#import "GoodsCell.h"

@interface GoodsListViewController (){
    NSArray *_dataArray;
}

@end

@implementation GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"已预定期望流向";
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshData];
}

- (void)getDataList{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    NSString *uid = [[GlobalData sharedGlobalData] userinfoDic][@"UserID"];
    [parameters addEntriesFromDictionary:@{@"userid":uid}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sch/my",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  _dataArray = responseObject;
                                  [self.tableView reloadData];
                                  [SVProgressHUD dismiss];
                              }];
 
}

- (void)deleteGoodsBook:(NSIndexPath *)indexPath{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"Scheduleid":_dataArray[indexPath.row][@"ScheduleID"]}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sch/del",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  [SVProgressHUD dismiss];
                                  if ([responseObject[@"result"] integerValue] == 1) {
                                      [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                      [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  //删除对应数据的cell
                                  }else{
                                      [SVProgressHUD showErrorWithStatus:@"删除失败"];
                                  }
                              }];
}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GoodsCell";
    
    GoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"GoodsCell" owner:self options:nil];
        cell = (GoodsCell *)[array objectAtIndex:0];
    }
    [cell configUIWithData:_dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GoodsDetailViewController *vc = [[GoodsDetailViewController alloc] initWithNibName:nil bundle:nil];
    vc.goodsID = _dataArray[indexPath.row][@"ScheduleID"];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
/*删除用到的函数*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self deleteGoodsBook:indexPath];
    }
}

//允许长按菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
